﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WF.Model;
using WF.DAL;
using WF.Common;
using System.ComponentModel;

namespace WF.BusinessObject
{
    ///// <summary>
    /////  逻辑层基类。提供与数据访问层的选择。

    ///// </summary>
    //[Obsolete("请使用 BOBase<T,K>和BOBase")]
    //public class BOBase<T> where T : class,ICCHREntity, new()
    //{
    //    protected IDataLayer _db;
    //    protected string _LastError = string.Empty;

    //    //数据库连接串
    //    private string _ConnectionString = string.Empty;

    //    //LINQ2 DataContext
    //    private WFDBDataContext _DataContext = null;

    //    //SQL语句的输出跟踪

    //    private InfoTextWriter _SQLTextLogger = new InfoTextWriter();

    //    public BOBase()
    //    {
    //        _db = new LINQDataLayer();
    //        _DataContext = ((LINQDataLayer)this._db).DataContext;
    //        _DataContext.Log = _SQLTextLogger;
    //    }

    //    public BOBase(IDataLayer DateLayer)
    //    {
    //        _db = DateLayer;
    //        _DataContext = ((LINQDataLayer)this._db).DataContext;
    //        _DataContext.Log = _SQLTextLogger;
    //    }

    //    public BOBase(WFDBDataContext DataContext)
    //    {
    //        _db = new LINQDataLayer();
    //        if (this._db is LINQDataLayer)
    //        {
    //            ((LINQDataLayer)this._db).DataContext = DataContext;
    //        }
    //        _DataContext.Log = _SQLTextLogger;
    //    }

    //    public string GetLastError()
    //    {
    //        return _LastError;
    //    }

    //    public virtual List<T> GetAll()
    //    {
    //        return null;
    //    }

    //    public virtual T GetById(string ID)
    //    {
    //        T result = new T();
    //        result.ID = ID;
    //        if (!result.GetDetail(_db))
    //        {
    //            _LastError = _db.GetLastError();
    //            return null;
    //        };
    //        return result;
    //    }

    //    public virtual bool Add(T obj)
    //    {
    //        if (!obj.Add(_db))
    //        {
    //            _LastError = _db.GetLastError();
    //            return false;
    //        }
    //        return true;
    //    }

    //    public virtual bool Update(T obj)
    //    {
    //        if (!obj.Update(_db))
    //        {
    //            _LastError = _db.GetLastError();
    //            return false;
    //        }
    //        return true;
    //    }

    //    public virtual bool Delete(T obj)
    //    {
    //        if (!obj.Delete(_db))
    //        {
    //            _LastError = _db.GetLastError();
    //            return false;
    //        }
    //        return true;
    //    }

    //    /// <summary>
    //    /// DataContext
    //    /// </summary>
    //    public WFDBDataContext DataContext
    //    {
    //        get
    //        {
    //            if (null == _DataContext) InitializeDataContext();
    //            return _DataContext;
    //        }
    //        //set { _DataContext = value; }
    //    }

    //    /// <summary>
    //    /// 初始化DataContext
    //    /// </summary>
    //    private void InitializeDataContext()
    //    {
    //        if (string.Empty == _ConnectionString)
    //        {
    //            _DataContext = new WFDBDataContext();
    //            if (this._db is LINQDataLayer)
    //            {
    //                ((LINQDataLayer)this._db).DataContext = _DataContext;
    //            }
    //        }
    //        else
    //        {
    //            _DataContext = new WFDBDataContext(_ConnectionString);
    //        }
    //    }
    //}

    /// <summary>
    /// 逻辑层基类。提供与数据访问层的选择。

    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="K"></typeparam>
    public abstract class BOBase<T, K>
        where T : EntityBase<K>, new()
        where K : class,INotifyPropertyChanging, INotifyPropertyChanged, new()
    {
        //LINQ2 DataContext
        private WFDBDataContext _DataContext = null;

        //数据库连接串
        private string _ConnectionString = string.Empty;

        //SQL语句的输出跟踪

        private InfoTextWriter _SQLTextLogger;

        /// <summary>
        /// 构造函数

        /// </summary>
        public BOBase()
        {
            SecurityManager = new SecurityManager(DataContext);
            SessionManager = new SessionManager(DataContext);
        }

        /// <summary>
        /// 构造函数

        /// </summary>
        /// <param name="DataContext"></param>
        public BOBase(WFDBDataContext DataContext)
        {
            _DataContext = DataContext;
            SecurityManager = new SecurityManager(DataContext);
            SessionManager = new SessionManager(DataContext);
            _SQLTextLogger = new InfoTextWriter();
            _DataContext.Log = _SQLTextLogger;

        }

        /// <summary>
        /// DataContext
        /// </summary>
        public WFDBDataContext DataContext
        {
            get
            {
                if (null == _DataContext) InitializeDataContext();
                return _DataContext;
            }
        }

        private SessionManager sessionManager;

        public SessionManager SessionManager
        {
            get { return sessionManager; }
            set { sessionManager = value; }
        }

        private SecurityManager securityManager;

        public SecurityManager SecurityManager
        {
            get { return securityManager; }
            set { securityManager = value; }
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="modelEntity"></param>
        public virtual string Add(T modelEntity)
        {
            if (null == modelEntity.ID)
            {
                modelEntity.ID = CreateEntityID();
            }
            DataContext.GetTable<K>().InsertOnSubmit(modelEntity.DBEntity);
            DataContext.SubmitChanges();
            return modelEntity.ID;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="modelEntity"></param>
        public virtual void Update(T modelEntity)
        {
            T orgModelEntity = GetById(modelEntity.ID);
            K orgDbEntity = orgModelEntity.DBEntity;
            modelEntity.UpdateDBEntity(orgDbEntity);
            DataContext.SubmitChanges();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="modelEntity"></param>
        public virtual void Delete(T modelEntity)
        {
            T obj = GetById(modelEntity.ID);
            DataContext.GetTable<K>().DeleteOnSubmit(obj.DBEntity);
            DataContext.SubmitChanges();
        }

        public virtual void Delete(List<T> modelEntityList)
        {
            List<K> ls = new List<K>();
            foreach (T item in modelEntityList)
            {
                T obj = GetById(item.ID);
                ls.Add(obj.DBEntity);
            }
            DataContext.GetTable<K>().DeleteAllOnSubmit(ls);
            DataContext.SubmitChanges();
        }

        public virtual void Delete(List<string> IDs)
        {
            List<K> ls = new List<K>();
            foreach (string item in IDs)
            {
                T obj = GetById(item);
                ls.Add(obj.DBEntity);
            }
            DataContext.GetTable<K>().DeleteAllOnSubmit(ls);
            DataContext.SubmitChanges();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        public virtual void DeleteByID(string ID)
        {
            T obj = GetById(ID);
            DataContext.GetTable<K>().DeleteOnSubmit(obj.DBEntity);
            DataContext.SubmitChanges();
        }

        public virtual List<T> GetAll()
        {
            List<T> ret = new List<T>();
            foreach (K item in DataContext.GetTable<K>())
            {
                T obj = new T();
                obj.CopyFrom(item);
                ret.Add(obj);
            }
            return ret;
        }

        protected virtual List<Z> ToList<Z>(IEnumerable<Z> objs)
            where Z : class
        {
            if (null == objs)
            {
                return objs.ToList();
            }
            else
            {
                return new List<Z>();
            }
        }

        /// <summary>
        /// GetById abstract方法，需要由子类实现
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public abstract T GetById(string ID);

        /// <summary>
        /// 生成新的实体对象标识号

        /// </summary>
        /// <returns></returns>
        protected virtual string CreateEntityID()
        {
            return Guid.NewGuid().ToString();
        }

        /// <summary>
        /// 初始化DataContext
        /// </summary>
        private void InitializeDataContext()
        {
            if (string.Empty == _ConnectionString)
            {
                _DataContext = new WFDBDataContext();
            }
            else
            {
                _DataContext = new WFDBDataContext(_ConnectionString);
            }
            _SQLTextLogger = new InfoTextWriter();
            _DataContext.Log = _SQLTextLogger;
        }
    }

    /// <summary>
    /// 逻辑层基类

    /// </summary>
    public abstract class BOBase
    {
        //LINQ2 DataContext
        private WFDBDataContext _DataContext = null;

        //数据库连接串
        private string _ConnectionString = string.Empty;

        //SQL语句的输出跟踪

        private InfoTextWriter _SQLTextLogger;

        /// <summary>
        /// 构造函数

        /// </summary>
        public BOBase()
        {

        }

        /// <summary>
        /// 构造函数

        /// </summary>
        /// <param name="DataContext"></param>
        public BOBase(WFDBDataContext DataContext)
        {
            _DataContext = DataContext;
            _SQLTextLogger = new InfoTextWriter();
            _DataContext.Log = _SQLTextLogger;
        }

        /// <summary>
        /// DataContext
        /// </summary>
        public WFDBDataContext DataContext
        {
            get
            {
                if (null == _DataContext) InitializeDataContext();
                return _DataContext;
            }
        }

        /// <summary>
        /// 初始化DataContext
        /// </summary>
        private void InitializeDataContext()
        {
            if (string.Empty == _ConnectionString)
            {
                _DataContext = new WFDBDataContext();
            }
            else
            {
                _DataContext = new WFDBDataContext(_ConnectionString);
            }
            _SQLTextLogger = new InfoTextWriter();
            _DataContext.Log = _SQLTextLogger;
        }
    }
}
